package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentUserBindMobileBinding;
import com.jwdf.uda.event.IndividualEvent;
import com.jwdf.uda.event.UserEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.BindMobileAction;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.ui.fragment.user.personal.MobiMessageFragment;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.ToastUtils;
import com.jwdf.uda.utils.UdaUtils;

import retrofit2.Response;

/**
 * Created by Administrator on 2016/6/22.
 */
public class UserBindMobileActivity extends AbsTitleBarActivity {
    public static void start(Context context, String title, String mobile) {
        Intent intent = new Intent(context, UserBindMobileActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("mobile", mobile);
        context.startActivity(intent);
    }

    @Override
    protected String getTopTitle() {
        return getIntent().getStringExtra("title");
    }

    @Override
    protected BaseFragment onCreateContentFragment() {
        return new BindMobileFragment();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBusUtil.registerEvent(this);
    }
    @Override
    protected Bundle onPutArguments() {
        Bundle args = new Bundle();
        args.putBoolean("has_mobile", !TextUtils.isEmpty(getIntent().getStringExtra("mobile")));
        args.putString("mobile", getIntent().getStringExtra("mobile"));
        return args;
    }
    @Override
    protected void onDestroy() {
        EventBusUtil.unregisterEvent(this);
        super.onDestroy();
    }

    public void onEvent(UserEvent event) {
        switch (event.getEvent()) {
            case UserEvent.EVENT_STEP_1:
                Bundle step1_args = new Bundle();
                step1_args.putString("mobile", event.getExtra());
                switchFragment(new MobiMessageFragment(), step1_args);
                break;
        }
    }

    public void onEvent(IndividualEvent event) {
        finish();
    }

    public static class BindMobileFragment extends BaseFragment {
        FragmentUserBindMobileBinding mDataBinding;
        private TextWatcher mTextChangeListener = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //mDataBinding.inputEdit.setSelection(s.length());
            }

            @Override
            public void afterTextChanged(Editable s) {
                mDataBinding.ivClearInput.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
                mDataBinding.tvBindPhone.setVisibility(View.GONE);
                if(TextUtils.isEmpty(s)){
                    mDataBinding.btnAuthCode.setEnabled(false);
                    mDataBinding.btnAuthCode.setClickable(false);
                }else {
                    mDataBinding.btnAuthCode.setEnabled(true);
                    mDataBinding.btnAuthCode.setClickable(true);
                }
            }
        };
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            mDataBinding = FragmentUserBindMobileBinding.inflate(inflater, container, false);
            initViews();
            return mDataBinding.getRoot();
        }
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            mDataBinding.setHasMobile(getArguments().getBoolean("has_mobile", false));
        }
        private void initViews() {
            mDataBinding.tvCurrentMobi.setText(getString(R.string.current_mobile, getArguments().getString("mobile", "")));
            if(!getArguments().getBoolean("has_mobile", false)) {
                mDataBinding.inputEdit.setHint("请填写你的手机号");
            }
            mDataBinding.inputEdit.addTextChangedListener(mTextChangeListener);
            mDataBinding.ivClearInput.setOnClickListener(createOnClickListener());
            mDataBinding.btnAuthCode.setOnClickListener(createOnClickListener());
        }
        private View.OnClickListener createOnClickListener() {
            return new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (R.id.btn_auth_code == v.getId()) {
//                        mDataBinding.ivClearInput.setVisibility(View.GONE);
                        UdaUtils.hideInputMethod(getContext(), mDataBinding.inputEdit);
                        String mobile = mDataBinding.inputEdit.getText().toString().trim();
                        if (TextUtils.isEmpty(mobile)) {
                            ToastUtils.show(getContext(), getString(R.string.mobile_can_not_be_null));
                            return;
                        }
                        if (!UdaUtils.isMobile(mobile)) {
                            ToastUtils.show(getContext(), getString(R.string.please_correct_the_mobile));
                            return;
                        }
                        bindMobile(mobile);
                    } else if (R.id.iv_clear_input == v.getId()) {
                        mDataBinding.inputEdit.setText("");
                        mDataBinding.tvBindPhone.setVisibility(View.GONE);
                    }
                }
            };
        }
        private void bindMobile(final String mobile) {
            showProgressDialog();
            onNewRequestCall(BindMobileAction.newInstance(getContext(), mobile).request(new AHttpService.IResCallback<PbBean.PbResUserBindMobile>() {
                @Override
                public void onCallback(int resultCode, Response<PbBean.PbResUserBindMobile> response, PbBean.PbBaseServerInfo baseServerInfo) {
                    if(getView() != null) {
                        closeProgressDialog();
                        if(HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                            mDataBinding.tvBindPhone.setVisibility(View.VISIBLE);
                            return;
                        }
                        UserEvent event = new UserEvent();
                        event.setEvent(UserEvent.EVENT_STEP_1);
                        event.setExtra(mobile);
                        EventBusUtil.post(event);
                    }
                }
            }));
        }

    }
}
